;; (global-srecode-minor-mode 1)            ; Enable template insertion menu
;; ;; 设置company-mode
;; (autoload 'company-mode "company" nil t)
;; (semantic-mode 1)
;; (global-ede-mode 1)
;; (global-semantic-show-unmatched-syntax-mode 1)
;; (global-semantic-show-parser-state-mode 1)
;; (defun my-semantic-hook ()
;;   (message "load my-semantic-hook")
;;   (global-semantic-mru-bookmark-mode 1)
;;   (local-set-key [(f12)] 'semantic-ia-fast-jump-or-back)
;;   (local-set-key [(shift f12)] 'semantic-ia-fast-jump-back)
;;   (local-set-key (kbd "M-/") 'semantic-complete-analyze-inline)
;;   ;; (local-set-key "." 'semantic-complete-self-insert)
;;   ;; (local-set-key ">" 'semantic-complete-self-insert)
;;   )
;; (add-hook 'c++-mode-hook 'my-semantic-hook)

;; CC-mode配置  http://cc-mode.sourceforge.net/
(require 'cc-mode)
(c-set-offset 'inline-open 0)
(c-set-offset 'friend '-)
(c-set-offset 'substatement-open 0)
;; 根据后缀判断所用的mode
;; 注意：我在这里把.h, .hpp., .c, .cpp都关联到了c++-mode
(setq auto-mode-alist
      (append '(("\\.h$" . c++-mode)) auto-mode-alist))
(setq auto-mode-alist
      (append '(("\\.hpp$" . c++-mode)) auto-mode-alist))
(setq auto-mode-alist
      (append '(("\\.c$" . c++-mode)) auto-mode-alist))
(setq auto-mode-alist
      (cons '("\\.cpp$" . c++-mode) auto-mode-alist))
;; 我的C/C++语言编辑策略
(defun my-c++-mode-hook()
  ;;按键定义`
  (define-key c-mode-base-map [(control \`)] 'hs-toggle-hiding)
  (define-key c-mode-base-map [(return)] 'newline-and-indent)
  (define-key c-mode-base-map [(f9)] 'compile)
  (define-key c-mode-base-map [(f11)] 'gdb)
  (define-key c-mode-base-map [(meta \`)] 'c-indent-command)
  ;;预处理设置
  (setq c-macro-shrink-window-flag t)
  (setq c-macro-preprocessor "cpp")
  (setq c-macro-cppflags " ")
  (setq c-macro-prompt-flag t)
  (setq hs-minor-mode t)
  (setq abbrev-mode t)
  (c-set-style "stroustrup")
  (c-set-offset 'innamespace 0)
  )
(add-hook 'c++-mode-hook 'my-c++-mode-hook)

;; 在C模式、C++模式中用Space代替tab
(add-hook 'c-mode-hook 'space-replace-tab)
(add-hook 'c++-mode-hook 'space-replace-tab)

;; 高亮结尾白空格
;; 设置颜色
(custom-set-faces
 '(my-trailing-space-face ((((class color)) (:background "grey30"))) t))
;; '(my-long-line-face ((((class color)) (:background "grey30"))) t))
;; 给mode增加高亮关键字函数
(defun highlight-hook (mode)
  (font-lock-add-keywords
   mode
   '(("^.*[^ \t\n]\\([ \t]+\\)$" (1 'my-trailing-space-face append)))))
;; ("^.\\{81\\}\\(.+\\)$" (1 'my-long-line-face append)))))
;; 對指定模式高亮結尾空白
(highlight-hook 'c-mode)
(highlight-hook 'cc-mode)
(highlight-hook 'c++-mode)
;; 对指定模式使用"超过80列高亮"功能
(add-hook 'c-mode-hook 'highlight-80+-mode)
(add-hook 'cc-mode-hook 'highlight-80+-mode)
(add-hook 'c++-mode-hook 'highlight-80+-mode)

;; 配置doxymacs
(add-to-list 'load-path "~/.emacs.d/lisp/doxymacs/share/emacs/site-lisp/")
(autoload 'doxymacs-mode "doxymacs" nil t)
(add-hook 'c-mode-common-hook 'doxymacs-mode)

;; 使用cmake-mode
(setq load-path (cons (expand-file-name "/dir/with/cmake-mode") load-path))
(autoload 'cmake-mode "cmake-mode" nil t)
(setq auto-mode-alist
      (append '(("CMakeLists\\.txt\\'" . cmake-mode)
                ("\\.cmake\\'" . cmake-mode))
              auto-mode-alist))

;; flymake
(defun flymake-display-current-error ()
  "Display errors/warnings under cursor."
  (interactive)
  (let ((ovs (overlays-in (point) (1+ (point)))))
    (catch 'found
      (dolist (ov ovs)
        (when (flymake-overlay-p ov)
          (message (overlay-get ov 'help-echo))
          (throw 'found t))))))
(defun flymake-goto-next-error-disp ()
  "Go to next error in err ring, then display error/warning."
  (interactive)
  (flymake-goto-next-error)
  (flymake-display-current-error))
(defun flymake-goto-prev-error-disp ()
  "Go to previous error in err ring, then display error/warning."
  (interactive)
  (flymake-goto-prev-error)
  (flymake-display-current-error))
(defvar flymake-mode-map (make-sparse-keymap))
(define-key flymake-mode-map "\C-cfn" 'flymake-goto-next-error-disp)
(define-key flymake-mode-map "\C-cfp" 'flymake-goto-prev-error-disp)
(define-key flymake-mode-map "\C-cfs"
  'flymake-display-err-menu-for-current-line)
(or (assoc 'flymake-mode minor-mode-map-alist)
    (setq minor-mode-map-alist
          (cons (cons 'flymake-mode flymake-mode-map)
                minor-mode-map-alist)))
(setq flymake-run-in-place t)

(provide 'jerry-cpp)
